/**
 * Created by xieze on 2016/12/22.
 * email xieze333@163.com
 */

// 正则策略
var validateRules = {
    isNonEmpty: function(value, errorMsg) {
            // console.log("validata",value,errorMsg);
        //不能为空
        if (typeof (value) == "number") {
            if(!value){
                return errorMsg;
            };
        }else{
            if(value == 'null' || !value.length){
                return errorMsg;
            };
        }
    },
    minLength: function(value, length, errorMsg) {
        //大于
        if (value.length < length) {
            return errorMsg;
        }
    },
    maxLength: function(value, length, errorMsg) {
        //小于
        if (value.length < length) {
            return errorMsg;
        }
    },
    isMobile: function(value, errorMsg) {
        //是否为手机号码
        if (!/(^1[0-9]{10}$)/.test(value)) {
            return errorMsg;
        }
    },
    iscphm: function(value, errorMsg) {
        console.log(value)
        //是否为车牌号码
        if (!/(^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$)/.test(value)) {
            return errorMsg;
        }
    },
    //只能输入英文，数字
    isNumEn:function (value,errorMsg) {
        // console.log(123)
        if(!/(^[a-zA-Z0-9]+$)/.test(value)){
            return errorMsg;
        }
    },
    //只能输入数字
    isNum:function (value,errorMsg) {
        // console.log(123)/^[0-4]\d{0,2}$/  /(^[^5]\d{0,2}$)|^[5]{0,1}$/
        if(!/(^[0-4]\d{0,2}$)|^5\d{0,1}$|500$|^[6-9]\d{0,1}$/.test(value)){
            return errorMsg;
        }
    },
    isEmail: function(value, errorMsg) {
        //是否为邮箱
        if (!/(^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$)/.test(value)) {
            return errorMsg;
        }
    },
    between: function(value, range, errorMsg) {
        //大于小于
        var min = parseInt(range.split('-')[0]);
        var max = parseInt(range.split('-')[1]);
        if (value.length < min || value.length > max) {
            return errorMsg;
        }
    },
    isNumBetween: function(value, range, errorMsg) {
        //大于小于isNumBetween:0-500||onlyNum
        var min = parseFloat(range.split('-')[0]);
        var max = parseFloat(range.split('-')[1]);
        if (value < min || value > max) {
            return errorMsg;
        }
    },
    onlyEn: function(value, errorMsg) {
        //纯英文
        if (!/^[A-Za-z]+$/.test(value)) {

        }
    },
    onlyZh: function(value, errorMsg) {
        //纯中文
        if (!/^[\u4e00-\u9fa5]+$/.test(value)) {
            return errorMsg;
        }
    },
    onlyNum: function(value, errorMsg) {
        //数字包含小数
        if (!/^[0-9]+([.][0-9]+){0,1}$/.test(value)) {
            return errorMsg;
        }
    },
    onlyInt: function(value, errorMsg) {
        //整数
        if (!/^[0-9]*$/.test(value)) {
            return errorMsg;
        }
    },
    isChecked: function(value, errorMsg, el) {
        var i = 0;
        var $collection = $(el).find('input:checked');
        if(!$collection.length){
            return errorMsg;
        }
    }
};
var validFunc = {

    validate : function(option){

        // console.log(option.label);
        // console.log($(option),81);

        var $this, dataValid, errCollection, validLen, errMsgAry, strategyAry, strategy, errMsg;
        $(option.el).find(".validata").parent().find(".g-clr").removeClass("in")
        $this = $(option.el).find(".validata");
        // console.log($this,22222)
        dataValid = "isNonEmpty";
        // console.log(dataValid);
        // console.log($this);
        errCollection = "此项不能为空！";
        // 默认验证条件为非空
        if(option.regexp){
            dataValid += "||"+option.regexp;
            errCollection += "||"+option.errMsg;
        }
        validLen = dataValid.split('||');
        // console.log(validLen);
        errMsgAry = errCollection.split("||");

        // console.log(errMsgAry);
        for (var i = 0; i < validLen.length; i++) {
            strategyAry = validLen[i].split(':');
            strategy = strategyAry.shift();
            strategyAry.unshift(""+option.enter_value || "");
            strategyAry.push(errMsgAry[i]);

            errMsg = validateRules[strategy].apply(this, strategyAry);

            // 弹出提示
            if (errMsg) {
                // console.log($this)
                $this.attr('data-status', 0);
                this.showMsg($this, errMsg, 2);
                return false;
            }
        }
        if (!errMsg) {
            $this.attr('data-status', 1);
            $this.parent().find('.valid_message').removeClass("in");
            return true;
            // setting.successTip ? this.showMsg($this, '', 1) : $this.parent().find('.valid_message').remove();
        };


    },
    showMsg : function(target, msg, status) {
        //status
        // 0 : tip
        // 1 : success
        // 2 : error
        var _current = status ? (status > 1 ? 'error' : 'success') : 'tip';
        // var $context = target.parent();
        var $context = target;
        var $msg = $context.find('.valid_message');
        // console.log($msg,132)
        if($msg.length >= 0){
            $msg = $('<div class="valid_message popover fade top  g-clr"><div class="arrow"></div><div class="popover-content">' + msg + '</div></div>');
            // $context.append($msg)
            $context.after($msg)
            if($(".valid_message").css("display")=="block"){
                if($("body")[0].attachEvents) {
                    $("body")[0].attachEvents("click", function() {
                        $(".valid_message").removeClass("in")
                    }, true);
                } else {
                    $("body")[0].addEventListener("click", function() {
                        $(".valid_message").removeClass("in")
                    }, true);
                }
            }
        }
        $context.removeClass('success tip error').addClass(_current);
        $msg.addClass("in");
    },
    check   : function(item){
        var flag = true;
        $.each(item,function(i,n){
            if(n.required == true){
                if(!validFunc.validate(n)){ flag = false;}
            }
        });
        return flag;
    }
}

export default validFunc;
